package com.yiduo.outpost.core.model.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @param <T>
 * @author GitEgg
 * @ClassName: PageResult
 * @Description: 通用分页返回
 * @date
 */
@Data
@Schema(description = "通用分页响应类")
public class PageResult<T> {
    @Schema(description = "是否成功", required = true)
    private boolean success;
    @Schema(description = "响应代码", required = true)
    private int code;
    @Schema(description = "提示信息", required = true)
    private String message;
    @Schema(description = "总数量", required = true)
    private long total;
    @Schema(description = "分页数据")
    private List<T> data;
    @Schema(description = "分页信息", required = true)
    private Page page;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class Page {
        private long total;
        private long index;
        private Long size;
    }

    private PageResult(Page page, List<T> rows,ResultCodeEnum resultCodeEnum) {
        this.page = page;
        this.data = rows;
        this.success = resultCodeEnum.SUCCESS.equals(resultCodeEnum);
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
    }

    private PageResult(long total, long index, long size, List<T> rows,ResultCodeEnum resultCodeEnum) {
        this(new Page(total, index, size), rows,resultCodeEnum);
    }

    public static <T> PageResult<T> ok(long total, long index, long size, List<T> rows) {
        return new PageResult(total,index,size,rows,ResultCodeEnum.SUCCESS);
    }

    public static <T> PageResult<T> error(ResultCodeEnum resultCodeEnum){
        return new PageResult<>(null,null,resultCodeEnum);
    }
}